/*
 *   Clovis
 *   Copyright (C) 2008 SFWOL
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "INIFileReader.h"

namespace clovis {

	namespace datas {

		INIFileReader::INIFileReader(core::CString input_file){

			this->file = input_file;
			this->current_line = 0;

			std::ifstream file( input_file.toCharArray() );

			if(file){

				std::string line;

				while (std::getline(file, line)){

					this->current_line++;
					this->parseLine(line);

				}

			}else{

				core::CString error = "File not found : " + input_file.toStdString();
				io::Log::logOn(io::WARNING,error);

			}

		}

		void INIFileReader::parseLine(core::CString line){

			line.wash(); // Remove white spaces avec tabs before and after the string

			if(	line.substr(0, 1) != "#" && line.size() != 0){

				if(line.substr(0, 1) != "["){

					// VARIABLE

					core::CString var_name = line.substr(0, line.findFirstOf("=") - 1);
					var_name.wash();

					core::CString var_value = line.substr(line.findFirstOf("=") + 1, line.size() - line.findFirstOf("="));
					var_value.wash();

					std::cout << "Variable on line " << this->current_line.toInt() << " : " << var_name.toCharArray() << " = " << var_value.toCharArray() << std::endl;

				}else{

					// SECTION
					core::CString section = line.substr(1, line.size() - 2);

					std::cout << "Section on line " << this->current_line.toInt() << " : " << section.toCharArray() << std::endl;

				}

			}

		}

	};

};
